﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Web.Cms.Membership;
using System.Web.Configuration;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics.CodeAnalysis;

namespace Proggy.Web.Cms.Context
{
    /// <summary>
    /// Back end context
    /// </summary>
    public class MembershipContext : IDisposable
    {
        #region constants

        internal const string MEMBERSHIP_CONTEXT_KEY = "CurrentMemberContext";
        internal const string MEMBERSHIP_SET_KEY = "CurrentMemberSet";
        internal const string MEMBERSHIP_KEY = "CurrentMember";

        #endregion

        #region static

        /// <summary>
        /// Context for current request
        /// </summary>
        public static MembershipContext Current
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                {
                    return (MembershipContext)HttpContext.Current.Session[MembershipContext.MEMBERSHIP_CONTEXT_KEY];
                }

                return null;
            }
        } 

        /// <summary>
        /// Private field
        /// </summary>
        private static HttpContextBase httpContextBase = HttpContext.Current == null ? null :
            (HttpContextBase)new HttpContextWrapper(HttpContext.Current);

        /// <summary>
        /// The root url
        /// </summary>
        public static string RootUrl
        {
            get
            {
                if (string.IsNullOrEmpty(_rootUrl))
                {
                    _rootUrl = FrontEndRequestContext.GetRootUrl();
                }

                return _rootUrl;
            }
        }

        /// <summary>
        /// Backing field
        /// </summary>
        private static string _rootUrl;

        /// <summary>
        /// The authentication section from web.config
        /// </summary>
        public static AuthenticationSection ConfigAuthenticationSection
        {
            get
            {
                if (_authSection == null)
                {
                    try
                    {
                        // get config
                        var config = WebConfigurationManager.OpenWebConfiguration(RootUrl + "web.config");

                        // set
                        _authSection = (AuthenticationSection)config.GetSection("system.web/authentication");
                    }
                    catch { 
                        //
                    }
                }

                return _authSection;
            }
        }

        /// <summary>
        /// Backing field
        /// </summary>
        private static AuthenticationSection _authSection;

        /// <summary>
        /// Current auth mode
        /// </summary>
        public static AuthenticationMode CurrentAuthenticationMode { get { return ConfigAuthenticationSection == null ? AuthenticationMode.None : ConfigAuthenticationSection.Mode; } }

        #endregion

        #region non-static

        /// <summary>
        /// The currently logged in member
        /// </summary>
        public ProggyMember CurrentMember
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                {
                    return (ProggyMember)HttpContext.Current.Session[MembershipContext.MEMBERSHIP_KEY];
                }

                return null;
            }
        }

        #endregion

        #region disposing

        /// <summary>
        /// Is disposed?
        /// </summary>
        private bool _disposed;

        /// <summary>
        /// Dispose
        /// </summary>
        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
        public void Dispose()
        {
            HttpContext.Current.Session.Remove(MEMBERSHIP_CONTEXT_KEY);
            HttpContext.Current.Session.Remove(MEMBERSHIP_SET_KEY);
            Dispose(true /* disposing */);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// True if object is disposed
        /// </summary>
        public bool IsDisposed { get { return this._disposed; } }

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                _disposed = true;
            }
        }

        #endregion        
    }
}
